package com.example.system.controller;

import com.example.common.core.domain.R;
import com.example.common.core.enums.ResultCode;
import com.example.system.domain.LoginDTO;
import com.example.system.domain.SysAdminSaveDTO;
import com.example.system.domain.SysUserVO;
import com.example.system.services.ISysAdminServices;
import com.mysql.cj.util.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2025-02-10
 * Time: 21:10
 */

// url 暂定为 sysAdmin/login
// 返回状态码 0 为失败 1 为成功
@RestController
@RequestMapping("/sysAdmin")
@Tag(name = "管理员层的API") // 给 Swagger 中取标签
public class SysAdminController {

    // 注入 services 层容器
    @Autowired
    private ISysAdminServices iSysAdminServices;

    // 登录需要返回成功还是失败, 若错误需要返回错误信息
    // 采用 PostMapping 出于安全考虑, GetMapping 传入的账号和密码在浏览器中会被明文解析

    /**
     *
     * @param loginDTO 封装的登录用户
     * @return 登录不需要返回响应数据, 泛型中为 void
     */
    // 写入 swagger 文档说明
    @PostMapping("/login")
    @Operation(summary = "管理系统登录", description = "根据账号密码进行管理员登录") // 对接口进行说明和描述
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3102", description = "⽤⼾不存在")
    @ApiResponse(responseCode = "3103", description = "⽤⼾名或密码错误")
    public R<Void> login (@RequestBody LoginDTO loginDTO) {

        if(StringUtils.isNullOrEmpty(loginDTO.getUserAccount())) {
            return R.fail(ResultCode.NOT_ENTER_ACCOUNT);
        }

        if(StringUtils.isNullOrEmpty(loginDTO.getPassword())) {
            return R.fail(ResultCode.NOT_ENTER_PASSWORD);
        }

        return iSysAdminServices.login(loginDTO.getUserAccount(), loginDTO.getPassword());

    }

    /**
     * 新增管理员操作
     * @param sysAdminSaveDTO 新增管理员封装实体类
     */
    @Operation(summary = "新增管理员", description = "根据提供的信息新增管理员⽤⼾")
    @PostMapping("/add")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾已存在")
    public R<Void> add(@RequestBody SysAdminSaveDTO sysAdminSaveDTO) {
        return null;
    }


    /**
     * 删除管理员操作
     * @param userId 管理员 id
     */
    @DeleteMapping("/{deUser}")
    @Operation(summary = "删除⽤⼾", description = "通过⽤⼾id删除⽤⼾")
    @ApiResponse(responseCode = "1000", description = "成功删除⽤⼾")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.PATH, description = "⽤⼾ID")
                    })
    public R<Void> delete(@PathVariable Long userId) {
        return null;
    }


    /**
     * 查询指定管理员详情
     * @param userId 管理员id
     * @param sex 管理员性别
     */
    @Operation(summary = "⽤⼾详情", description = "根据查询条件查询⽤⼾详情")
    @GetMapping("/detail")
    @ApiResponse(responseCode = "1000", description = "成功获取⽤⼾信息")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101", description = "⽤⼾不存在")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description =
                    "⽤⼾ID"),
            @Parameter(name = "sex", in = ParameterIn.QUERY, description = "⽤⼾性别")
                    })

    public R<SysUserVO> detail(Long userId, @RequestParam(required = false)
            String sex) {
        return null;
    }
}


